package com.mx.recycle_adapt.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.mx.adapt.pager.MXBannerAdapt
import com.mx.adapt.pager.indicator.MXRoundIndicator
import com.mx.adapt.pager.transformer.MXEmptyTransForm
import com.mx.adapt.pager.transformer.MXScaleTransForm
import com.mx.recycle_adapt.R
import com.mx.recycle_adapt.databinding.AdaptBannerIndexBinding
import kotlinx.android.synthetic.main.activity_banner_view.*

class BannerActivity : AppCompatActivity() {
    private val adapt = SpanCountAdapt()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_banner_view)
        bannerView.setAdapt(adapt)

        adapt.list.addAll((0 until 5))
        adapt.notifyDataSetChanged()
        bannerView.setIndicator(MXRoundIndicator(2f))
        bannerView.startAutoScroll()
        bannerView.setOnPageSelect { index ->
            infoTxv.text = "当前选中：$index"
        }
        adapt.setItemClick { position, record ->
        }

        showTransformMod.setOnCheckedChangeListener { _, isChecked ->
            bannerView.setPageTransformer(if (isChecked) MXScaleTransForm() else MXEmptyTransForm())
        }

        showIndicatorMod.setOnCheckedChangeListener { _, isChecked ->
            bannerView.setIndicatorShow(isChecked)
        }
        loopMod.setOnCheckedChangeListener { _, isChecked ->
            adapt.setLoopMode(isChecked)
        }
        loopMod.isChecked = true
    }


    class SpanCountAdapt : MXBannerAdapt<Int>() {
        val res = arrayListOf(
            R.mipmap.item1,
            R.mipmap.item2,
            R.mipmap.item3,
            R.mipmap.item4,
            R.mipmap.item5,
            R.mipmap.item6,
            R.mipmap.item1,
            R.mipmap.item2,
            R.mipmap.item3,
            R.mipmap.item4,
            R.mipmap.item5,
            R.mipmap.item6,
        )

        override fun createItem(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): ViewBinding {
            return AdaptBannerIndexBinding.inflate(inflater, parent, false)
        }

        override fun bindView(position: Int, binding: ViewBinding, record: Int) {
            binding as AdaptBannerIndexBinding
            binding.indexTxv.text = "" + (record + 1)
            binding.image.setImageResource(res.get(position % list.size))
        }
    }

    fun scrollToFirst(view: View) {
        bannerView.scrollToIndex(0, true)
    }

    fun addOneItem(view: View) {
        adapt.list.add(adapt.list.size)
        adapt.notifyDataSetChanged()
    }
}